/*
 * Copyright 2016-2036 the original author or authors.
 *
 * COMMERCIAL USE OF THIS SOFTWARE WITHOUT WARRANTY IS NOT ALLOWED.
 * Use is subject to license terms! You can distribute a copy of this software
 * to others for free. This software is a non-profit and open-source project.
 * Any contribution to this project will make it better.
 * All rights reserved! Owned by Stephen Liu.
 *
 */
package com.github.raffle.behavior.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 执行董事，被观察者，执行董事发出一个通告，通知到所有其管辖的部门
 * @author ste7en.liu@gmail.com
 * @since 1/27/2016
 */
public class Supervisor implements Subject {
	//观察者列表
	private List<Observer> observers;
	//命令列表
	private List<String> orders;
	
	public Supervisor() {
		observers = new ArrayList<>();
		orders = new ArrayList<>();
	}
	
	/**
	 * 发布一个命令，通知所有的观察者
	 * @param order 命令
	 */
	public void addOrder(String order) {
		if (!orders.contains(order)) {
			orders.add(order);
			inform();
		}
	}
	
	@Override
	public void attach(Observer observer) {
		if (!observers.contains(observer)) {
			observers.add(observer);
		}
	}

	@Override
	public void detach(Observer observer) {
		if (observers.contains(observer)) {
			observers.remove(observer);
		}
	}

	@Override
	public void inform() {
		observers.forEach(e->e.callback(this, e.getClass().getName()));
	}

}
